package com.weishi.project.training.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.utils.StringUtils;
import com.weishi.project.training.domain.HeartRate;
import com.weishi.project.training.domain.HeartRateCount;
import com.weishi.project.training.domain.HeartRateRecord;
import com.weishi.project.training.domain.TrainingAchievement;
import com.weishi.project.training.mapper.HeartRateRecordMapper;
import com.weishi.project.training.service.IHeartRateCountService;
import com.weishi.project.training.service.IHeartRateRecordService;
import com.weishi.project.training.service.IHeartRateService;
import com.weishi.project.training.service.ITrainingAchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 心率计次数据Service业务层处理
 */
@Service
public class HeartRateRecordServiceImpl extends ServiceImpl<HeartRateRecordMapper, HeartRateRecord> implements IHeartRateRecordService {

    @Autowired
    private HeartRateRecordMapper heartRateRecordMapper;

    @Autowired
    private IHeartRateCountService heartRateCountService;

    @Autowired
    private IHeartRateService heartRateService;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     * 查询心率计次数据列表
     */
    @Override
    public List<HeartRateRecord> selectHeartRateRecordList(HeartRateRecord heartRateRecord) {
        return heartRateRecordMapper.selectHeartRateRecordList(heartRateRecord);
    }

    @Override
    public List<HeartRateRecord> selectByMap(Map<String, Object> heartRateParams) {
        return heartRateRecordMapper.selectByMap(heartRateParams);
    }

    @Override
    public void addRecord(Integer channelId) {
        //每个运动员的分段心率
        Map<Integer,List<Integer>> sportHeartRate = new HashMap<>();

        List<HeartRateCount> heartRateCounts = heartRateCountService.qryCountByChannel(channelId);
        if(heartRateCounts!=null && heartRateCounts.size()>0){
            int index = 1;//第一个计次
            for(HeartRateCount count : heartRateCounts){
                HeartRateRecord params = new HeartRateRecord();
                params.setHeartRateCountId(count.getId());
                List<HeartRateRecord> heartRateRecords = heartRateRecordMapper.selectHeartRateRecordList(params);
                for(HeartRateRecord heartRateRecord : heartRateRecords){
                    aggregatedData(heartRateRecord,index,channelId);
                    heartRateRecordMapper.updateById(heartRateRecord);

                    List<Integer> temp = sportHeartRate.get(heartRateRecord.getSportsmanId());
                    if(temp==null){
                        temp = new ArrayList<>();
                        sportHeartRate.put(heartRateRecord.getSportsmanId(),temp);
                    }
                    temp.add(heartRateRecord.getHeartRateAvg());
                }
                index++;
            }

        }

        //更新成绩表
        for(Integer sportsmanId : sportHeartRate.keySet()){
            List<Integer> heartRates = sportHeartRate.get(sportsmanId);
            StringBuffer heartRateAvg = new StringBuffer();
            for(Integer heartRate : heartRates){
                heartRateAvg.append(heartRate).append(" ");
            }
            trainingAchievementService.update(Wrappers.<TrainingAchievement>lambdaUpdate()
                    .set(TrainingAchievement::getHeartRate, StringUtils.isEmpty(heartRateAvg) ? 0 : heartRateAvg.toString().trim())
                    .eq(TrainingAchievement::getChannelId , channelId)
                    .eq(TrainingAchievement::getSportsmanId , sportsmanId));
        }
    }

    /**
     * 合计心率详细数据
     */
    private void aggregatedData(HeartRateRecord heartRateRecord, int index, int channelId){
        HeartRate params = new HeartRate();
        params.setHeartRateCountId(index);
        params.setChannelId(channelId);
        params.setSportsmanId(heartRateRecord.getSportsmanId());
        List<HeartRate> heartRates = heartRateService.selectHeartRateList(params);
        if(heartRates==null || heartRates.size()==0){
            return;
        }

        int maxHR = 0;
        int totalHR = 0;
        List<Map<String,Object>> jsondate = new ArrayList<>();
        for(HeartRate heartRate : heartRates){
            if(maxHR < heartRate.getHr()){
                maxHR = heartRate.getHr();
            }
            totalHR += heartRate.getHr();
            Map<String,Object> temp = new HashMap<>();
            temp.put("time",heartRate.getTime());
            temp.put("hr",heartRate.getHr());
            jsondate.add(temp);
        }

        heartRateRecord.setHeartRateMax(maxHR);
        heartRateRecord.setHeartRateAvg(totalHR/heartRates.size());
        heartRateRecord.setJsondata(JSONArray.toJSONString(jsondate));
    }
}